﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
using System.Collections;
using System.Data.SqlClient;

namespace cninfo
{
    public partial class GroupInfo
    {      
        private class GroupResult
        {
            public SqlString ticker;
            public SqlString name;
            public SqlString title;
            public SqlString url;
            public SqlString size;
            public SqlString _date;
            public SqlString msg;

            public GroupResult(SqlString ticker, SqlString name, SqlString title, SqlString url,
                                SqlString size, SqlString _date, SqlString msg)
                                                                                                               
            {
                this.ticker = ticker;
                this.name = name;
                this.title = title;
                this.url = url;
                this.size = size;
                this._date = _date;
                this.msg = msg;
            }
        }

        [SqlFunction(
       DataAccess = DataAccessKind.Read,
       SystemDataAccess = SystemDataAccessKind.Read,
       FillRowMethodName = "InitMethod",
       TableDefinition = "ticker nvarchar(32),name nvarchar(32),title nvarchar(1024),url nvarchar(1024),"
                + "size nvarchar(32),_date nvarchar(64),msg nvarchar(2048)")]
        public static IEnumerable f_download_notice(SqlString dt1, SqlString dt2)
        {
            ArrayList resultCollection = new ArrayList();

            MySpider myspider = new MySpider();
            myspider.StartDate = string.Format("{0}-{1}-{2}", 
                    dt1.Value.Substring(0, 4), dt1.Value.Substring(4, 2), dt1.Value.Substring(6, 2));
            myspider.EndDate = string.Format("{0}-{1}-{2}",
                    dt2.Value.Substring(0, 4), dt2.Value.Substring(4, 2), dt2.Value.Substring(6, 2));  
            //myspider.StartDate = "2011-05-30";
            //myspider.EndDate = "2011-05-30";
            List<Notice> list = myspider.CollectNotice();

            foreach (Notice n in list)
            {
                GroupResult result = new GroupResult(n.Ticker, n.Name, n.Title, n.Url, n.Size, n.Date, n.Msg);
                resultCollection.Add(result);
            }

            return resultCollection;
        }

        public static void InitMethod(
                object indexParResultObj,
                out SqlString ticker,
                out SqlString name,
                out SqlString title,
                out SqlString url,
                out SqlString size,
                out SqlString _date,
                out SqlString msg)
        {
            GroupResult IndexResult = (GroupResult)indexParResultObj;

            ticker = IndexResult.ticker;
            name = IndexResult.name;
            title = IndexResult.title;
            url = IndexResult.url;
            size = IndexResult.size;
            _date = IndexResult._date;
            msg = IndexResult.msg;
        } 
    }
}
